/**
 * 
 */
package br.ufrgs.inf.cwsmart.matcher;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import br.ufrgs.inf.cwsmart.exception.FrameworkException;

/**
 * Interface that define the mart functionalities and rules
 * 
 * @author Rodrigo S Lumertz
 *
 */
public class MartProperties {
	
	private List<String> lstFunctionality;
	private Map<String, String> mapRule;

	public MartProperties() throws FrameworkException{
		lstFunctionality = new ArrayList<String>();
		mapRule = new HashMap<String, String>();		
		
		Properties props = new Properties();
		
		//try retrieve data from file
        try {
            
        	props.load(new FileInputStream("config//mart.properties"));

        	//funtionalities property is mandatory
        	if (props.getProperty("functionalities") != null) {
        		lstFunctionality = Arrays.asList(props.getProperty("functionalities").split(","));
        	} else {
        		throw new FrameworkException(FrameworkException.ErrorCode.MART_FUNCTIONALITIES_NOT_FOUND);
        	}
        	
        	//mart rules
        	Set<String> keys = props.stringPropertyNames();
        	for (String key : keys) {
				if (!key.equalsIgnoreCase("functionalities")) {									
					mapRule.put(key, props.getProperty(key));
				}				
			}        	
        } catch (IOException e) {
        	throw new FrameworkException(FrameworkException.ErrorCode.MART_PROPERTIES_FILE_NOT_FOUND);
        } catch (FrameworkException e) {
        	throw e;
        } catch (Exception e) {        	
        	throw new FrameworkException(FrameworkException.ErrorCode.MART_PROPERTIES_FILE_PARSING_ERROR);
		}
	}
	
	/**
	 * Gets the current mart functionalities
	 * 
	 * @return A list of functionalities
	 */
	public List<String> getMartFunctionalities() {
		return this.lstFunctionality;
	}
	
	/**
	 * Gets the current mart rules
	 * 
	 * @return A map of rules names and values
	 */
	public Map<String, String> getMartRules() {
		return this.mapRule;
	}
}
